﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace GaSaiGon.GUI
{
    public partial class frmQuanLyGaTau : Form
    {
        public frmQuanLyGaTau()
        {
            InitializeComponent();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmThemGaTau frmthem = new frmThemGaTau();
            frmthem.sender = new frmThemGaTau.SEND(getString);
            frmthem.Show();
        }

        private void frmQuanLyGaTau_Load(object sender, EventArgs e)
        {

            GaTauBUS gataubus = new GaTauBUS();
            dgvDanhSach.DataSource = gataubus.LoadGaTau();
            dgvDanhSach.AllowUserToAddRows = false;
        }

        private void btnrefresh_Click(object sender, EventArgs e)
        {
            GaTauBUS gataubus = new GaTauBUS();
            dgvDanhSach.DataSource = gataubus.LoadGaTau();
        }

        private void dgvDanhSach_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Convert.ToString(dgvDanhSach.CurrentRow.Cells["matau"].Value);
        }

        private void btnCapnhat_Click(object sender, EventArgs e)
        {
            string matau = Convert.ToString(dgvDanhSach.CurrentRow.Cells["matau"].Value);
            GaTauBUS gatauBus = new GaTauBUS();
            GaTauDTO gatauDto = new GaTauDTO();
            gatauDto.LayThongTinGaTau(matau);
            string Matau = (string)gatauBus.LayThongTinGaTau(gatauDto).Rows[0]["MaTau"];
            frmCapNhatGaTau frm = new frmCapNhatGaTau();
            frm.Tag = Matau;
            frm.sender = new frmCapNhatGaTau.SEND(getString);
            frm.Show();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            string timkiem = txtTimKiem.Text;
            GaTauBUS gataubus = new GaTauBUS();
            GaTauDTO gataudto = new GaTauDTO();
            gataudto.TimKiemGaTau(timkiem);
            gataubus.TimKiemGaTau(gataudto);
            dgvDanhSach.DataSource = gataubus.TimKiemGaTau(gataudto);
            dgvDanhSach.AllowUserToAddRows = false;
        }

        private void btnChiTiet_Click(object sender, EventArgs e)
        {
            string matau = Convert.ToString(dgvDanhSach.CurrentRow.Cells["matau"].Value);
            GaTauBUS gatauBus = new GaTauBUS();
            GaTauDTO gatauDto = new GaTauDTO();
            gatauDto.LayThongTinGaTau(matau);
            string Matau = (string)gatauBus.LayThongTinGaTau(gatauDto).Rows[0]["MaTau"];
            frmChiTietGaTau frm = new frmChiTietGaTau();
            frm.Tag = Matau;
            frm.Show();
        }
        public void getString(string s)
        {
            if (s == "Close")
            {
                GaTauBUS gataubus = new GaTauBUS();
                dgvDanhSach.DataSource = gataubus.LoadGaTau();
            }
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            string matau = (string)dgvDanhSach.CurrentRow.Cells["MaTau"].Value;
            GaTauBUS GTBUS = new GaTauBUS();
            GaTauDTO GTDTO = new GaTauDTO();

            GTDTO.KiemTraChuyenTau(matau);
            GTBUS.KiemTraChuyenTau(GTDTO);

            if (GTBUS.KiemTraChuyenTau(GTDTO).Rows.Count == 1)
            {
                MessageBox.Show("Không được xóa tàu này vì tàu này đang hoạt động!");
            }
            else
            {
                GTDTO.XoaGaTau(matau);
                GTBUS.XoaGaTau(GTDTO);

                MessageBox.Show("Xóa thành công!");
                GaTauBUS gataubus = new GaTauBUS();
                dgvDanhSach.DataSource = gataubus.LoadGaTau();
            }
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btnXuLyVe_Click(object sender, EventArgs e)
        {
            frmXuLyVe frm = new frmXuLyVe();
            frm.Show();
        }

    }
}
